<template>
    <div class="sub-layout org-edit-content">
        <div class="sub-sidebar">
            <div class="search-bar">
                <div class="search-box">
                    <el-select v-model="search.value" placeholder="搜索用户..." filterable remote :remote-method="searchMethod" :loading="search.loading"
                        @change="searchChange">
                        <el-option v-for="item in search.list" :key="item.id" :label="item.userName" :value="item"></el-option>
                    </el-select>
                    <i class="icon i-search"></i>
                </div>
                <permission class="add" permission-key="salesDept:addSalesDept">
                    <a @click="showEditLayer(0)">
                        <i class="icon i-plus"></i>
                    </a>
                </permission>
            </div>
            <div class="side-nav">
                <tree-view :model="treeData" @on-selected="treeSelected" @on-toggle-expand="getOrgLevel"></tree-view>
            </div>
        </div>
        <div class="sub-main">
            <div class="content">
                <div class="section section-panel">
                    <div class="panel-title">
                        <span class="title">{{title}}</span>
                    </div>
                    <div class="panel-content">
                        <el-form class="form-box" inline :ref="form.ref" :rules="form.rules" :model="orgUser" @submit.prevent>
                            <el-form-item label="人员姓名" prop="userName">
                                <!-- <el-input v-if="sourceType==2" disabled type="text" v-model="orgUser.userName"></el-input> -->
                                <el-input type="text" v-model="orgUser.userName"></el-input>
                            </el-form-item>
                            <el-form-item label="邮箱地址" prop="email">
                                <!-- <el-input v-if="sourceType==2" disabled type="text" v-model="orgUser.email"></el-input> -->
                                <el-input type="text" v-model="orgUser.email"></el-input>
                            </el-form-item>
                            <el-form-item label="职位" prop="position">
                                <!-- <el-input v-if="sourceType==2" disabled type="text" v-model="orgUser.position"></el-input> -->
                                <el-input type="text" v-model="orgUser.position"></el-input>
                            </el-form-item>
                            <el-form-item label="身份证号" prop="identity">
                                <!-- <el-input v-if="sourceType==2" disabled type="text" v-model="orgUser.identity"></el-input> -->
                                <el-input type="text" v-model="orgUser.identity"></el-input>
                            </el-form-item>
                            <el-form-item label="手机号" prop="phone">
                                <!-- <el-input v-if="sourceType==2" disabled type="text" v-model="orgUser.phone"></el-input> -->
                                <el-input type="text" v-model="orgUser.phone"></el-input>
                            </el-form-item>
                            <el-form-item label="入职时间" prop="entryTime">
                                <el-date-picker v-model="form.entryTime" value-format="yyyy-MM-dd" type="date">
                                </el-date-picker>
                            </el-form-item>
                            <el-form-item label="所属职场" prop="theirWork">
                                <el-input type="text" v-model="orgUser.theirWork"></el-input>
                            </el-form-item>
                        </el-form>
                        <div class="form-btn">
                            <permission permission-key="orgUser:addOrgUser">
                                <button @click.prevent="sumbitForm">保存</button>
                            </permission>
                            <button class="btn-cancel" @click="goBack">返回</button>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <!-- 编辑layer -->
        <layer :data="editLayerData" @on-cancel="closeEditLayer" @on-ok="editLayerConfirm">
            <div class="el-form">
                <div class="el-form-item is-required" :class="{'is-error':editLayerData.error}">
                    <label class="el-form-item__label" style="width: 100px;">公司名称</label>
                    <div class="el-form-item__content" style="margin-left: 112px;">
                        <div class="el-input">
                            <input autocomplete="off" type="text" validateevent="true" class="el-input__inner" placeholder="请输入公司名称" v-model="editLayerData.name"
                                @blur="layerInputBlur">
                        </div>
                        <div class="el-form-item__error" v-if="editLayerData.error">请输入公司名称</div>
                    </div>
                </div>
            </div>
        </layer>
    </div>
</template>

<script>
    import './orgEdit.less'
    import orgManage from '@/mixins/orgManage'
    import { formatDate } from '@/mixins/filters'
    export default {
        name: 'orgEdit',
        mixins: [orgManage],
        data() {
            return {
                title: this.$route.query.userid ? '修改人员' : '添加人员',
                form: {
                    ref: 'orgEditForm',
                    rules: {
                        userName: [
                            { required: true, message: '请输入人员姓名', trigger: 'blur' }
                        ],
                        email: [
                            { required: true, message: '请输入邮箱地址', trigger: 'blur' },
                            { type: 'email', message: '邮箱地址格式不正确', trigger: 'blur' }
                        ],
                        identity: [
                            // { required: true, message: '请输入身份证号', trigger: 'blur' },
                            { pattern: /^\d{15}$|^\d{17}[0-9Xx]$/, message: '身份证号格式不正确', trigger: 'blur' }
                        ],
                        phone: [
                            { required: true, message: '请输入手机号', trigger: 'blur' },
                            { pattern: /^1[3|5|6|7|8|9]\d{9}$/, message: '手机号格式不正确', trigger: 'blur' }
                        ]
                    },
                    // 入职时间
                    entryTime: ''
                },
                orgUser: {}
            }
        },
        computed: {
            userId() {
                return this.$route.query.userid || 0
            },
            deptCode() {
                return this.$route.query.code || 0
            },
            sourceType() {
                return this.$route.query.source || 0
            }
        },
        mounted() {
            this.$store.dispatch('setBreadData', [
                {
                    name: '系统管理'
                },
                {
                    name: '组织架构管理',
                    path: '/system/org'
                },
                {
                    name: '编辑组织架构'
                }
            ])
            if (this.userId) {
                api.getOrgUser({ id: this.userId }).then(res => {
                    this.orgUser = res
                    this.form.entryTime = res.hiredate < 0 ? '' : formatDate(res.hiredate, 'yyyy-MM-dd')
                })
            }
        },
        methods: {
            // 返回
            goBack() {
                this.$router.go(-1)
            },
            // 保存
            sumbitForm() {
                this.$refs[this.form.ref].validate((valid) => {
                    if (valid) {
                        this.editOrgUser()
                    } else {
                        this.$msg.error('表单验证失败!')
                    }
                })
            },
            //架构用户
            editOrgUser() {
                var _this = this;
                this.orgUser.salesDeptCode = this.deptCode
                if (this.form.entryTime) {
                    if ((this.form.entryTime.toString().indexOf('-') > -1 || this.form.entryTime.toString().indexOf('/') > -1)) {
                        this.orgUser.hiredate = Date.parse(this.form.entryTime)
                    }
                    else {
                        this.orgUser.hiredate = this.form.entryTime
                    }
                }
                else {
                    this.orgUser.hiredate = '1900-01-01'
                }
                api.editOrgUser({
                    type: this.userId ? 1 : 0,
                    data: this.orgUser,
                }).then(res => {
                    if (this.userId) {
                        this.$msg.success('修改成功')
                    }
                    else {
                        this.$msg.success('添加成功')
                    }
                    this.$router.go(-1)
                }).catch(res => {
                    console.log(res)
                })
            }
        }
    }
</script>